import random 
ch = int(input('Введите четырёхзначное число: ')) 
max = 0 
if ch > 9999 or ch < 1000: 
    print ('0') 
def prost(n): 
    if n <= 1: 
        return False 
    for _ in range(5): 
        a = random.randint(1, n-1) 
        if pow(a, n-1, n) != 1: 
            return False 
        return True  
 
 
tfc = ch // 1000 
if prost(tfc) == True: 
    max = tfc#1 
 
 
p = ch // 100 
if prost(p) == True and ts > max: 
    max =p#2 
 
 
bnk = ch // 10 
if prost(bnk) == True and tsd > max: 
    max =bnk#3 
 
 
a = ch 
if prost(a) == True and a > max: 
    max =a#4 
 
 
 x = ch // 100 % 10 
if prost(x) == True and s > max: 
    max = x#5 
 
 
mr = ch // 10 % 100 
if prost(mr ) == True and sd > max: 
    max =mr#6 
 
 
fgh = ch % 1000 
if prost(fgh) == True and sde > max: 
    max = fgh#7 
 
 
zv = ch // 10 %10 
if prost(zv) == True and d > max: 
    max = zv#8 
 
 
fg = ch % 100 
if prost(fg) == True and de > max: 
    max = fg#9 
 
 
k = ch % 10 
if prost(k) == True and e > max: 
    max = k#10 
 
 
print(max)